#include <stdlib.h>

#include "lib.h"
#include "getrand.h"

#include "slotguts.h"

#include "myowguts.h"
#include "myowcommon.h"
using namespace MyowCommon;

bool8 
MyowSlotGuts::isValidGuts()
{
	return getGutsId() >= 1100 && getGutsId() < 1200;
}

uint8
MyowSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint64 resultMASK = ((uint64)1 << EVT_LOSER);

	int32 i, blank = 0, luck = 0, flower = 0, 
			 coi = 0,   sun = 0,  dragon = 0,
			 whitecat = 0,		  purplecat = 0,
			 yellowcat = 0,		  greencat = 0,
			 pinkcat = 0;


	for (i = 0; i < getReelCount(); i++)
	{
		switch ( getIcon(i, stops[i])->iconid )
		{
			case BLANK:
				blank++;
				break;
				
			case LUCK:
				luck++;
				break;
				
			case FLOWER:
				flower++;
				break;
				
			case COI:
				coi++;
				break;
				
			case SUN:
				sun++;
				break;
				
			case DRAGON:
				dragon++;
				break;
				
			case WHITECAT:
				whitecat++;
				break;
				
			case PURPLECAT:
				purplecat++;
				break;
				
			case YELLOWCAT:
				yellowcat++;
				break;
				
			case GREENCAT:
				greencat++;
				break;
				
			case PINKCAT:
				pinkcat++;
				break;

			default:
				ASSERT(!"Bad Icon Type");
				break;

		}
	}

	int32 cats = whitecat + purplecat + yellowcat + greencat + pinkcat;

	if (blank == 3)
		resultMASK |= ((uint64)1 << EVT_3BLANKS);
	else if (blank == 0)
		resultMASK |= ((uint64)1 << EVT_3ICONS);


	if (luck >= 1)
		resultMASK |= ((uint64)1 << EVT_1LUCK);

	if (luck >= 2)
		resultMASK |= ((uint64)1 << EVT_2LUCK);

	if (luck >= 3)
		resultMASK |= ((uint64)1 << EVT_3LUCK);


	if (flower >= 1)
		resultMASK |= ((uint64)1 << EVT_1FLOWER);

	if (flower >= 2)
		resultMASK |= ((uint64)1 << EVT_2FLOWER);

	if (flower >= 3)
		resultMASK |= ((uint64)1 << EVT_3FLOWER);


	if (coi >= 1)
		resultMASK |= ((uint64)1 << EVT_1COI);

	if (coi >= 2)
		resultMASK |= ((uint64)1 << EVT_2COI);

	if (coi >= 3)
		resultMASK |= ((uint64)1 << EVT_3COI);


	if (sun >= 1)
		resultMASK |= ((uint64)1 << EVT_1SUN);

	if (sun >= 2)
		resultMASK |= ((uint64)1 << EVT_2SUN);

	if (sun >= 3)
		resultMASK |= ((uint64)1 << EVT_3SUN);


	if (dragon >= 1)
		resultMASK |= ((uint64)1 << EVT_1DRAGON);

	if (dragon >= 2)
		resultMASK |= ((uint64)1 << EVT_2DRAGON);

	if (dragon >= 3)
		resultMASK |= ((uint64)1 << EVT_3DRAGON);


	if (whitecat >= 1)
		resultMASK |= ((uint64)1 << EVT_1WHITECAT);

	if (whitecat >= 2)
		resultMASK |= ((uint64)1 << EVT_2WHITECAT);

	if (whitecat >= 3)
		resultMASK |= ((uint64)1 << EVT_3WHITECAT);


	if (purplecat >= 1)
		resultMASK |= ((uint64)1 << EVT_1PURPLECAT);

	if (purplecat >= 2)
		resultMASK |= ((uint64)1 << EVT_2PURPLECAT);

	if (purplecat >= 3)
		resultMASK |= ((uint64)1 << EVT_3PURPLECAT);


	if (yellowcat >= 1)
		resultMASK |= ((uint64)1 << EVT_1YELLOWCAT);

	if (yellowcat >= 2)
		resultMASK |= ((uint64)1 << EVT_2YELLOWCAT);

	if (yellowcat >= 3)
		resultMASK |= ((uint64)1 << EVT_3YELLOWCAT);


	if (greencat >= 1)
		resultMASK |= ((uint64)1 << EVT_1GREENCAT);

	if (greencat >= 2)
		resultMASK |= ((uint64)1 << EVT_2GREENCAT);

	if (greencat >= 3)
		resultMASK |= ((uint64)1 << EVT_3GREENCAT);

	
	if (pinkcat >= 1)
		resultMASK |= ((uint64)1 << EVT_1PINKCAT);

	if (pinkcat >= 2)
		resultMASK |= ((uint64)1 << EVT_2PINKCAT);

	if (pinkcat >= 3)
		resultMASK |= ((uint64)1 << EVT_3PINKCAT);


	if (cats >= 1)
		resultMASK |= ((uint64)1 << EVT_1CAT);

	if (cats >= 2)
		resultMASK |= ((uint64)1 << EVT_2CAT);

	if (cats >= 3)
		resultMASK |= ((uint64)1 << EVT_3CAT);


	uint8 payTable = m_subGame->getData(SGIDX_LASTPAYTABLE);

	ASSERT(payTable > 0);

	//offset the result by the paytable number to find the correct payout
	uint8  ptjump = NUMEVENTS * (payTable-1);

	uint8  result = EVT_LOSER;
	int32  payOut = 0;

	//loop thru all events that we won and choose the one with the highest payout
	for(i=0;i<NUMEVENTS;i++)
	{
		//check all payouts
		if ((resultMASK & ((uint64)1 << i)) != 0 &&
			getPayout(bet, ptjump + i)->payoutAmount > payOut)
		{
			result = i;
			payOut = getPayout(bet, ptjump + i)->payoutAmount;
		}
	}

#ifdef DEBUGVERBOSE
	DEBUGMSG(("MyowGuts.cpp - Payout: %s",
              getPayout(bet, ptjump + result)->payoutName));
#endif

	return ptjump + result;
}

void
MyowSlotGuts::playBonusGame(uint8 bonusGameType, int32 bonusDataCount, int32 *bonusData)
{
	DEBUGMSG(("bonusGameType: %d", bonusGameType));
	DEBUGMSG(("bonusDataCount: %d", bonusDataCount));

	for(int32 i=0;i<bonusDataCount;i++)
	{
		DEBUGMSG(("  bonusData: %d", bonusData[i]));
	}

	if (bonusGameType == 0)	//paytable requested
	{
		if (bonusDataCount == 1)
		{
			DEBUGMSG(("Paytable: %d", bonusData[0]));

			if (bonusData[0] >= 0 && bonusData[0] <= NUM_PAYTABLES)
			{
				m_subGame->update(SGIDX_LASTPAYTABLE, bonusData[0]);
			}
			else
			{
				ASSERT(0);
			}
		}
		else
		{
			//bad msg
			ASSERT(0);
		}
	}
	else
	{
		//unkown bonus game
		ASSERT(0);
	}
}

void
MyowSlotGuts::loadSubGameData()
{
	SlotGuts::loadSubGameData();

	// If the paytable is not set, default it to a random paytable
	if ( m_subGame->getData(SGIDX_LASTPAYTABLE) < 1 )
    {
        m_subGame->update(SGIDX_LASTPAYTABLE, getrand() % NUM_PAYTABLES + 1);

		//don't save the paytable here that way if they don't spin they will 
		//keep getting random paytables until they spin for the first time
    }
}

#ifdef DEBUGSPIN
void MyowSlotGuts::Debug_SetLoserStops( uint8* pStops )
{
	pStops[0] = 18;
	pStops[1] = 9;
	pStops[2] = 1;
}
#endif